package com.immunology.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;

import com.immunology.core.BaseDao;
import com.immunology.core.BaseTransformer;
import com.immunology.core.GenericDao;
import com.immunology.database.DbColumns;
import com.immunology.database.transformers.UserTransformer;
import com.immunology.model.User;

public class UserDao extends BaseDao<User> implements GenericDao<User>{
	public UserDao(BaseTransformer<User> transformer) {
		super(transformer, new String("USER"));
	}

	@Override
	public void insert(User user) {
		Connection connection = connectionManager.getConnection();
		StringBuilder query = new StringBuilder();
		query.append("INSERT INTO ");
		query.append(tableName);
		query.append(" (FIRST_NAME, MIDDLE_NAME, LAST_NAME, LOGIN, PASSWORD, ROLE, POST)");
		query.append(" VALUES(?, ?, ?, ?, ?, ?, ?)");
		try {
//			user.setId(getNextId());
			PreparedStatement preparedStatement= (PreparedStatement) connection
					.prepareStatement(query.toString());
			preparedStatement.setString(DbColumns.FIRST_COLUMN, user.getFirstName());
			preparedStatement.setString(DbColumns.SECOND_COLUMN, user.getMiddleName());
			preparedStatement.setString(DbColumns.THIRD_COLUMN, user.getLastName());
			preparedStatement.setString(DbColumns.FOURTH_COLUMN, user.getLogin());
			preparedStatement.setString(DbColumns.FIFTH_COLUMN, user.getPassword());
			preparedStatement.setString(DbColumns.SIXTH_COLUMN, user.getRole().value());
			preparedStatement.setString(DbColumns.SEVENTH_COLUMN, user.getPost());
			preparedStatement.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			connectionManager.closeConnection(connection);
		}
	}

	public User getUserByLoginAndPassword(String login, String password) {
		User user = null;
		Connection connection = connectionManager.getConnection();
		StringBuilder query = new StringBuilder();
		query.append("SELECT ID FROM ");
		query.append(tableName);
		query.append(" WHERE LOGIN = ? AND PASSWORD = ?");
		try {
			PreparedStatement pstmt = connection.prepareStatement(query
					.toString());
			pstmt.setString(DbColumns.FIRST_COLUMN, login);
			pstmt.setString(DbColumns.SECOND_COLUMN, password);
			ResultSet resultSet = pstmt.executeQuery();
			if (!resultSet.next()) {
				return user;
			}
			int id = resultSet.getInt(DbColumns.FIRST_COLUMN);
			user = getById(id);
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			connectionManager.closeConnection(connection);
		}
		return user;
	}

	public boolean isLoginInDB(String login) {
		boolean result = false;
		Connection connection = connectionManager.getConnection();
		StringBuilder query = new StringBuilder();
		query.append("SELECT ID FROM ");
		query.append(tableName);
		query.append(" WHERE LOGIN = ?");
		try {
			PreparedStatement pstmt = connection.prepareStatement(query
					.toString());
			pstmt.setString(DbColumns.FIRST_COLUMN, login);
			ResultSet resultSet = pstmt.executeQuery();
			if (resultSet.next()) {
				result = true;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			connectionManager.closeConnection(connection);
		}
		return result;
	}

	@Override
	public void update(int id, User obj) {
		// TODO Auto-generated method stub

	}

	public boolean changeRole(int id, String role) {
		boolean result = false;
		Connection connection = connectionManager.getConnection();
		StringBuilder query = new StringBuilder();
		query.append("UPDATE ");
		query.append(tableName);
		query.append(" SET ROLE = ? WHERE ID = ?");
		try {
			PreparedStatement pstmt = (PreparedStatement) connection
					.prepareStatement(query.toString());
			pstmt.setString(DbColumns.FIRST_COLUMN, role.toLowerCase());
			pstmt.setInt(DbColumns.SECOND_COLUMN, id);
			pstmt.executeUpdate();
			result = true;
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			connectionManager.closeConnection(connection);
		}
		return result;
	}
	public List<User> search(String text) {
		List<User> users = new LinkedList<User>();
		Connection connection = connectionManager.getConnection();
		StringBuilder textSearch = new StringBuilder();
		textSearch.append("%");
		textSearch.append(text);
		textSearch.append("%");
		StringBuilder query = new StringBuilder();
		query.append("SELECT * FROM ");
		query.append(tableName);
		query.append(" WHERE UPPER(NICKNAME) LIKE ? ");
		query.append("OR UPPER(FIRST_NAME) LIKE ? OR UPPER(LAST_NAME) LIKE ?");
		try {
			PreparedStatement pstmt = (PreparedStatement) connection
					.prepareStatement(query.toString());
			pstmt.setString(1, textSearch.toString());
			pstmt.setString(2, textSearch.toString());
			pstmt.setString(3, textSearch.toString());
			ResultSet resultSet = pstmt.executeQuery();
			while (resultSet.next()) {
				users.add(new UserTransformer().transformTo(resultSet));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			connectionManager.closeConnection(connection);
		}
		return users;
	}

}
